var i;
$(function(){
	$("#txtParamValue").live("focusout", function(){
		$.ajax({
			type : 'post',
			dataType : 'text',
			contentType : "application/json",
			url : ctx + "/admin/base.do?action=1&value="+ $("#txtParamValue").val(),
			success : function(result) {
				if(result == "1"){
					$("#spanWarn").text("存在相同的类别").css("color","red");					
				}else if(result == "2"){
					$("#spanWarn").text("*类别必须以Menu结尾").css("color","red");	
				}else if(result == "0"){
					$("#spanWarn").text("*可以添加此类别").css("color","green");					
				}else if(result == "3"){
					$("#spanWarn").text("*类别的长度必须大于4位").css("color","red");					
				}
			}
		});
	});
	$("#btnNew").live("click", function(){
		i = $.layer({
		    type : 1,
		    title : false,
		    fix : false,
		    offset:['50px' , ''],
		    area : ['300px','250px'],
		    page : {dom : '#newDiv'}
		});
	});
});

function createObject(){
	var ParamValue = $("#txtParamValue").val();
	if(ParamValue == ""){
		layer.alert('类别不能为空', 2);
	}
	var Remark = $("#txtRemark").val();
	if(Remark == ""){
		layer.alert('备注不能为空', 2);
	}
	var param = new Object();
	param.paramValue = ParamValue;
	param.remark = Remark;
	var jsonData = JSON.stringify(param);
	$.ajax({
		type : 'post',
		dataType : 'json',
		contentType : "application/json",
		data : jsonData,
		url : ctx + "/admin/base.do?action=0",
		success : function(result) {
			$("#txtParamValue").val('');
			$("#txtRemark").val('');
			layer.close(i);
			window.location.href = "addMenu.jsp";
		}
	});
}

function validateExist(){
	var menu = $("#txtTableName").val();
	if(menu == ""){
		layer.alert('请输入菜单名', 2);
		return;
	}
	
	var type = $("#selectType").val();
	if(type == "0"){
		layer.alert('请选择类别', 2);
		return;
	}
	$.ajax({
		type : 'post',
		dataType : 'text',
		contentType : "application/json",
		url : ctx + "/admin/base.do?action=3&menu="+ menu +"&type="+ type,
		success : function(result) {
			if(result == "1"){
				$("#txtWarnTableName").text("存在相同的菜单").css("color","red");					
			}else {
				$("#txtWarnTableName").text("*ok").css("color","green");					
			}
		}
	});
}

function returnIndex(){
	window.location.href = "index.jsp";
}

function add(){
	var path = $("#txtpath").val();
	var menu = $("#txtTableName").val();
	if(menu == ""){
		$("#txtWarnTableName").text("*请输入菜单名").css("color", "red");
		return;
	}
	
	var type = $("#selectType").val();
	if(type == "0"){
		$("#txtWarnTableName").text("*请选择类别").css("color", "red");
		return;
	}
	
	type = type.split('-')[0];
	
	var param = new Object();
	param.paramName = type;
	param.paramValue = menu;
	param.remark = path;
	var jsonData = JSON.stringify(param);
	$.ajax({
		type : 'post',
		dataType : 'json',
		contentType : "application/json",
		data : jsonData,
		url : ctx + "/admin/base.do?action=3",
		success : function(result) {
			window.location.href = "index.jsp";
		}
	});
}

